﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Wedn.Core.Entity;
using Wedn.Core.Service;
using Wedn.Core.Utility;
using Wedn.TemplateEngine.UC;

namespace Wedn.Web.UI
{
    public class FrontPage : PageBase
    {
        ///// <summary>
        ///// 选项管理对象
        ///// </summary>
        //public OptionManager option = null;

        private string siteurl = Config.SiteUrl;
        /// <summary>
        /// 站点URL
        /// </summary>
        protected string SiteUrl
        {
            get { return siteurl; }
        }
        private string pageType = null;
        /// <summary>
        /// 页面类型
        /// </summary>
        protected string PageType
        {
            get { return pageType; }
        }
        private string themeName;
        /// <summary>
        /// 主题名称(文件夹)
        /// </summary>
        protected string ThemeName
        {
            get { return themeName; }
        }
        private string masterPath = null;
        /// <summary>
        /// 母版页路径
        /// </summary>
        protected string MasterPath
        {
            get { return masterPath; }
        }
        private string templatePath = null;
        /// <summary>
        /// 模板页路径
        /// </summary>
        protected string TemplatePath
        {
            get { return templatePath; }
        }
        private bool hasMaster = false;
        /// <summary>
        /// 当前加载主题是否包涵母版页
        /// </summary>
        protected bool HasMaster
        {
            get { return hasMaster; }
        }

        /// <summary>
        /// 模板操作类对象
        /// </summary>
        protected TemplateHelper th = null;// new TemplateHelper();
        protected FrontPage()
        {
            //记录开始时间
            QueryInfo.ExecuteTime = DateTime.Now;
            pageType = RequestHelper.QueryString("type", true);
            this.themeName = Setting.Theme;
            this.masterPath = string.Format("{0}themes/{1}/site.master", Config.SiteRoot, themeName);
            this.templatePath = string.Format("{0}themes/{1}/templates/", Config.SiteRoot, themeName);
        }
        /// <summary>
        /// 页面初始化事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreInit(EventArgs e)
        {
            hasMaster = LoadMaster();
            base.OnPreInit(e);
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
        #region 方法
        /// <summary>
        /// 是否存在母版页,有则加载母版页
        /// </summary>
        /// <returns>是否存在</returns>
        protected bool LoadMaster()
        {
            if (System.IO.File.Exists(Server.MapPath(masterPath)))
            {
                MasterPageFile = masterPath;
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新浏览次数
        /// </summary>
        public void UpdateViewCount()
        {
            int count = Convert.ToInt32(Setting.SiteViewCount) + 1;
            Setting.Dict["site_view_count"] = count;
            new OptionManager().UpdateValue("site_view_count", count.ToString());
            //string cookie = "isview";
            //int isview = StringHelper.StrToInt(PageUtils.GetCookie(cookie), 0);
            ////未访问或按刷新统计
            //if (isview == 0 || SettingManager.GetSetting().SiteTotalType == 1)
            //{
            //    StatisticsInfo stat = StatisticsManager.GetStatistics();
            //    stat.VisitCount += 1;
            //    StatisticsManager.UpdateStatistics();
            //}
            ////未访问
            //if (isview == 0 && SettingManager.GetSetting().SiteTotalType == 2)
            //{
            //    PageUtils.SetCookie(cookie, "1", 1440);
            //}
        }
        #endregion
        #region 废弃
        ///// <summary>
        ///// 根据页面类型加载模板
        ///// </summary>
        ///// <param name="pageType">页面类型</param>
        //public void Include(string pageType)
        //{
        //    //根据页面类型加载页面模版
        //    switch (pageType)
        //    {
        //        case "index":
        //            //需要虚拟路径,不能使用物理路径
        //            //templatePath = Server.MapPath(string.Format("{0}themes/{1}/templates/uc_index.ascx", Config.SiteRoot, themeName));
        //            templatePath += "index";
        //            break;
        //        case "post":
        //            templatePath += "index";
        //            break;
        //        default:
        //            templatePath += "index";
        //            break;
        //    }
        //    Control control = null;
        //    //判断是否存在模版文件, 支持两种后缀名(可拓展)
        //    if (System.IO.File.Exists(Server.MapPath(templatePath + ".ascx")))
        //    {
        //        control = LoadControl(templatePath + ".ascx");
        //    }
        //    else if (System.IO.File.Exists(Server.MapPath(templatePath + ".htm")))
        //    {
        //        control = LoadControl(templatePath + ".htm");
        //    }
        //    else if (System.IO.File.Exists(Server.MapPath(templatePath + ".template")))
        //    {
        //        control = LoadControl(templatePath + ".template");
        //    }
        //    else if (System.IO.File.Exists(Server.MapPath(templatePath + ".html")))
        //    {
        //        control = LoadControl(templatePath + ".html");
        //    }
        //}

        ///// <summary>
        ///// 输出提示,并终止页面
        ///// </summary>
        ///// <param name="title">标题</param>
        ///// <param name="msg">错误信息</param>
        //public void ResponseError(string title, string msg)
        //{
        //    ResponseError(title, msg, 500);
        //}

        //public void ResponseError(string title, string msg, int statusCode)
        //{
        //    string str = "<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>{0} - {1}</title></head><body style=\"font: normal 14px 微软雅黑\"><div style=\"border: 1px solid #00a303;background-color: #c2ffeb; width: 600px; padding: 8px 25px; margin: 10% auto;\"><h5>提示:</h5><h2>{2}</h2><h4><a href=\"{3}\" style=\"color: #808080;\">返回首页</a></h4></div><div style=\"text-align: center; padding: 5px 0; color: gray; font-size: 12px;\">Powered by <a href=\"http://www.wedn.net\" style=\"color: #808080;\" target=\"_blank\">Wedn.Net</a></div></body></html>";

        //    str = string.Format(str, title, OptionManager.Settings["SiteUrl"], msg, Config.SiteUrl);

        //    HttpContext.Current.Response.Clear();
        //    HttpContext.Current.Response.StatusCode = statusCode;
        //    HttpContext.Current.Response.Write(str);
        //    HttpContext.Current.Response.End();
        //}

        ///// <summary>
        ///// 错误处理
        ///// </summary>
        ///// <param name="e"></param>
        //protected override void OnError(EventArgs e)
        //{
        //    Exception ex = Server.GetLastError();
        //    if (ex != null)
        //    {
        //        ResponseError("程序出现错误", ex.Message);
        //        Server.ClearError();
        //    }
        //    base.OnError(e);
        //} 
        #endregion
    }
}
